package br.ufu.sdg12014.thriftpluresdb.client;

import java.util.Scanner;
import java.util.regex.Pattern;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Guilherme
 */
public class TextInterface {
    
    private static final String header;
    private static int       authenticationToken;
    
    static
    {
        String commands = "";
        for(String s : Tokens.tokens)
        {
            commands += s + ", ";
        }
        commands = commands.substring(0, commands.length() - 2);
        header = "~~~~~~~~~~~~~~~~~\nPluresDB Client\n~~~~~~~~~~~~~~~~~\nCommands:\n" + commands + "\n";
    }
    
    public static void execute(Action action)
    {
        System.out.println(header);
        authenticationToken = action.getToken();
        System.out.println("[INFO] Client token = " + authenticationToken + "\n");
        String req;
        String[] tokens;
        Scanner s = new Scanner(System.in);
        do
        {
            System.out.print("?> ");
            req = s.nextLine();
            tokens = Tokens.getTokens(req);
            tokens[0] = tokens[0].toLowerCase();
            System.out.println(Tokens.containsCmd(tokens[0]) ? executeCmd(tokens, action) : "[ERROR] Command not found");            
        } while(!tokens[0].equalsIgnoreCase("exit"));
    }
    
    private static String executeCmd(String[] tokens, Action action)
    {
        String resp = "[INFO] Bye!";
        String command = tokens[0].toLowerCase();
        if (command.equals("exit"))
        {
            return resp;
        }
        int key = Integer.parseInt(tokens[1]);
        switch(command)
        {
            case "create":
                resp = action.create(authenticationToken, key, tokens[2]);
                break;
            case "read":
                resp = action.read(authenticationToken, key);
                break;
            case "update":
                resp = action.update(authenticationToken, key, tokens[2]);
                break;
            case "delete":
                resp = action.delete(authenticationToken, key);
                break;
            default: break;
        }
        return resp;
    }
    
    
    static abstract class Tokens
    {
        static String[] tokens = new String[]{"create","read","update","delete","exit"};
        
        static String[] getTokens(String statement)
        {
            /* precisa ser melhorado */
            statement = statement.replaceFirst(Pattern.quote("("), ",");
            statement = statement.replaceAll(Pattern.quote(")"), ",");
            
            return statement.split(",");
        }
        
        static boolean containsCmd(String cmd)
        {
            for(String token : tokens)
            {
                if(token.equals(cmd))
                {
                    return true;
                }
            }
            return false;
        }
    }
    
    
    
}
